#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#                                                                   #
# @(#)74   1.14   src/csm/install/rmnode_client.perl, setup, csm_rameh, rameh0431a 3/29/04 09:18:18

#--------------------------------------------------------------------------------

=head1	rmnode.client


	Description: This command runs of the node throucg dsh

	AIX:	Remove the logs  on the node.
	Linux:	Removes the logs, csm, rsct file sets and /var/opt/csm/CFMlocal
		file from the node

	Return 0 or 1.


        Development notes:

        The html view of the pod headers in this file can be updated
        by running the command:  

                  tidypod <filename>

	The html veiw of the pod headers will be in the file ./<filename>.html
	and can be viewed with a browser.

	The tidy formatted file will be in <filename>.tdy

	Be sure to run tidypod and copy the tdy file to <filename> before checking
	in changes.

=cut

#--------------------------------------------------------------------------------

BEGIN    # load first at runtime
{
    use lib '/opt/csm/pm';
    use strict;
    use locale;

    $::MSGCAT     = 'csmInstall.cat';
    $::MSGSET     = 'csminstall';
    $::MSGMAPPATH = '/opt/csm/msgmaps';

}

use Getopt::Std;
use NodeUtils;
use CSMDefs;
use InstallUtils;

#--------------------------------------------------------------------------------

=head3	initialize	

	Initialize the global data

        Notes:

=cut

#--------------------------------------------------------------------------------

sub initialize
{

    # setup the global defs data
    InstallUtils->get_OSDefs();

    $::ostype    = InstallUtils->get_OSName;                 #AIX/Linux
    $::osdistr   = InstallUtils->get_DistributionName;       #RedHat
    $::osversion = InstallUtils->get_DistributionVersion;    #5.1.0/7.1/7.2
    $::arch      = InstallUtils->get_PkgArchitecture;        #i.86

    if ($::arch =~ /i.86/) { $::arch = "i386"; }

    $::csmvers = InstallUtils->get_CSMVersion("csm.core");

    @::csm_logfilelist;    # csm file, log list thate need to removed
    @::csm_rpmlist;        # Linux CSM RPM list to uninstall
    @::rsct_rpmlist;       # Linux RSCT RPM list to uninstall

    $::GLOBAL_EXIT = 0;
    $::logging     = 0;
    $::temp_remove_item;
    $::rsct_basic_Flg = 0;

}

#--------------------------------------------------------------------------------

=head3	getArgs	

        Notes:

=cut

#--------------------------------------------------------------------------------

sub getArgs
{
    if (!getopts('vVh'))
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'I', 'IMsgrmnodeClientNodeUsage');
        exit 1;
    }

    if ($::opt_h)
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'I', 'IMsgrmnodeClientNodeUsage');
        exit 0;
    }
    if ($::opt_v || $::opt_V)
    {
        if ($::opt_v) { $::VERBOSE = $::opt_v; }
        if ($::opt_V) { $::VERBOSE = $::opt_V; }
    }
}

#--------------------------------------------------------------------------------

=head3	get_install_lists

	Get the lists of files that we need to remove.

        Notes:

=cut

#--------------------------------------------------------------------------------

sub get_install_lists
{

    if ($::PLTFRM eq "AIX")
    {    # AIX lists of filesets to install
            # make sure we have the info we need
        if (($::ostype eq "") || ($::osversion eq "") || ($::csmvers eq ""))
        {
            NodeUtils->messageFromCat(
                                      'csmInstall.cat',   $::MSGMAPPATH,
                                      'csminstall',       'E',
                                      'EMsgNoConfigInfo', $::CSM_NODENAME
                                      );
            return $::NOK;
        }

        # get the appropriate lists of filesets/packages
        my %pkgdefs =
          InstallUtils->get_pkgdefs($::ostype, $::osdistr, $::osversion,
                                    $::arch,   "MgdNode",  $::csmvers);

        # log file list that need to uninstalled
        @::csm_logfilelist = @{$pkgdefs{logfiles}};
    }
    else
    {    # Linux lists of filesets to install
            # make sure we have the info we need
        if (   ($::ostype eq "")
            || ($::osdistr   eq "")
            || ($::osversion eq "")
            || ($::csmvers   eq ""))
        {
            NodeUtils->messageFromCat(
                                      'csmInstall.cat',   $::MSGMAPPATH,
                                      'csminstall',       'E',
                                      'EMsgNoConfigInfo', $::CSM_NODENAME
                                      );
            return $::NOK;
        }

        # get the appropriate lists of filesets/packages
        my %pkgdefs =
          InstallUtils->get_pkgdefs($::ostype, $::osdistr, $::osversion,
                                    $::arch,   "MgdNode",  $::csmvers);

        # CSM RPMs for Linux that need to Uninstalled
        @::csm_rpmlist = @{$pkgdefs{csm_unpackages}};
        @::csm_rpmlist = reverse @::csm_rpmlist;

        # rsct rpms that need to uninstalled
        @::rsct_rpmlist = @{$pkgdefs{rsct_unpackages}};
        @::rsct_rpmlist = reverse @::rsct_rpmlist;

        # check if the node is management server to it self.
        # if yes, than remove only client
        if (NodeUtils->isMgmtSvr())
        {
            @::csm_rpmlist  = ("csm.client");
            @::rsct_rpmlist = ();
        }

        # if the rsct.basic is installed than don't remove rsct
        NodeUtils->runcmd("$::RPMCMD -q rsct.basic", -1);
        if ($::RUNCMD_RC == 0)
        {
            @::rsct_rpmlist   = ();
            $::rsct_basic_Flg = 1;
        }

        # log file list that need to uninstalled
        @::csm_logfilelist = @{$pkgdefs{logfiles}};
    }
    return $::OK;
}

#--------------------------------------------------------------------------------

=head3	remove_files_dir

	Remove the directries and files passes.

        Notes:

=cut

#--------------------------------------------------------------------------------

sub remove_files_dir
{
    my ($file_name, undef) = @_;
    NodeUtils->runcmd("$::RM -rf $file_name", -1);
    if ($::RUNCMD_RC != 0)
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'W', 'EMsgCannotDelete', $file_name);
    }
    return $::RUNCMD_RC;
}

#--------------------------------------------------------------------------------

=head3	remove_rpm

	Remove the rpm from the system

        Notes:

=cut

#--------------------------------------------------------------------------------

sub remove_rpm
{
    my ($rpm_name, undef) = @_;
    NodeUtils->runcmd("$::RPMCMD -e $rpm_name", -1);
    if ($::RUNCMD_RC != 0)
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'W', 'EMsgRemoveRpm', $rpm_name);
    }
    return $::RUNCMD_RC;
}

#--------------------------------------------------------------------------------

=head3	remove_installp

	Remove the installp immage from the system

        Notes:

=cut

#--------------------------------------------------------------------------------

sub remove_installp
{
    my ($installp_img, undef) = @_;
    NodeUtils->runcmd("$::INSTALLP -u $installp_img", -1);
    if ($::RUNCMD_RC != 0)
    {
        NodeUtils->messageFromCat(
                                  'csmInstall.cat',        $::MSGMAPPATH,
                                  'csminstall',            'W',
                                  'EMsgRemoveInstallpImg', $installp_img
                                  );
    }
    return $::RUNCMD_RC;

}

#--------------------------------------------------------------------------------

=head3	Main	

        Notes:

=cut

#--------------------------------------------------------------------------------

{    # main

    my $ret_code = 0;

    # initialize the global data
    &initialize;

    # get/check command line args
    &getArgs;

    #  Get the lists of filesets and file that must be uninstalled or removed
    if (&get_install_lists != 0)
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'E', 'EMsgNO_LIST');
        $::GLOBAL_EXIT = 1;
        exit;
    }

    # Uninstalling and removing the files from the node
    if ($::PLTFRM eq "AIX")
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'I', 'IMsgUninstallingCsm');

        # Remove the log and otherfiles
        foreach $::temp_remove_item (@::csm_logfilelist)
        {
            $ret_code = &remove_files_dir($::temp_remove_item);
        }
    }
    else
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'I', 'IMsgUninstallingCsm');

        # Remove the Linux Cluster Support extensions, if present
        # Do not remove if node is management server for itself
        unless (   (($::osdistr =~ /RedHatEL-AS/) && ($::arch =~ /ppc64/))
                || NodeUtils->isMgmtSvr())
        {
            NodeUtils->runcmd("$::RMLCSEXT -A", 0);
        }

        # Remove the csm rpms
        foreach $::temp_remove_item (@::csm_rpmlist)
        {
            $ret_code = &remove_rpm($::temp_remove_item);
        }

        # Remove the rsct rpms
        foreach $::temp_remove_item (@::rsct_rpmlist)
        {
            $ret_code = &remove_rpm($::temp_remove_item);
        }

        # Remove the log and otherfiles
        foreach $::temp_remove_item (@::csm_logfilelist)
        {
            $ret_code = &remove_files_dir($::temp_remove_item);
        }

    }
    if ($::rsct_basic_Flg == 1)
    {
        NodeUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
                                  'I', 'IMsgDidnotUninstallRsct');
    }

    exit $ret_code;
}
